package com.ek.util;

import java.io.UnsupportedEncodingException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Utils {
    private static final transient Logger log = LoggerFactory.getLogger(Utils.class);

    /**
     * returns the byte array of encripted string.
     * @param s
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static byte[] computeSHA1FromString(String s) {
        byte[] input = {};
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.reset();
            input = digest.digest(s.getBytes("UTF8"));
        } catch (NoSuchAlgorithmException e) {
            log.error("computeSHA1FromString NoSuchAlgorithmException", e);
        } catch (UnsupportedEncodingException e) {
            log.error("computeSHA1FromStrings UnsupportedEncodingException", e);
        }
        return input;
    }

    /**
     * returns the string of ip address.
     * @return
     * @throws UnknownHostException
     */
    public static String getLocalHostIP() {
        try {
            return getIpFromAddress(InetAddress.getLocalHost().getAddress());
        } catch (UnknownHostException e) {
            log.error("getLocalHostIP UnknownHostException", e);
        }
        return "127.0.0.1";
    }

    private static String getIpFromAddress(byte[] ipAddr) throws UnknownHostException {
        String ipAddrStr = "";
        for (int i = 0; i < ipAddr.length; i++) {
            if (i > 0) {
                ipAddrStr += ".";
            }
            ipAddrStr += ipAddr[i] & 0xFF;
        }
        if (ipAddrStr.equalsIgnoreCase("")) {
            throw new IllegalStateException("Error getting IP.");
        }
        return ipAddrStr;
    }

    /**
     * gets the port of @{link {@link SocketAddress}.
     * @param address
     * @return port or -1 if unsuccessful.
     */
    public static int getPortOfSocketAddress(SocketAddress address) {
        if (address instanceof InetSocketAddress) {
            InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
            return inetSocketAddress.getPort();
        } else {
            return -1;
        }
    }

    public static String getAddressOfSocketAddress(SocketAddress address) throws UnknownHostException {
        if (address instanceof InetSocketAddress) {
            InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
            return getIpFromAddress(inetSocketAddress.getAddress().getAddress());
        } else {
            return "";
        }
    }

    public static boolean isLocalHost(InetAddress address) {
        try {
            for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
                NetworkInterface iface = ifaces.nextElement();
                for (Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses.hasMoreElements();) {
                    InetAddress temp = addresses.nextElement();
                    if (temp instanceof Inet4Address && temp.equals(address)) {
                        return true;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return false;
    }
}
